package com.bqtv.sport.ui.im

import android.content.Context
import android.content.Intent
import com.bqtv.sport.dialog.RedPackageDialog
import com.google.gson.Gson
import com.hjq.base.BaseActivity
import com.tencent.imsdk.v2.V2TIMManager
import com.tencent.imsdk.v2.V2TIMUserFullInfo
import com.tencent.imsdk.v2.V2TIMValueCallback
import com.tencent.qcloud.tuikit.tuichat.bean.CustomRedPackageMessage
import com.tencent.qcloud.tuikit.tuichat.bean.message.CustomRedPackageMessageBean
import com.tencent.qcloud.tuikit.tuichat.callback.ChatAppModule
import com.tencent.qcloud.tuikit.tuichat.callback.ChatAppModule.AppModuleCallback
import com.tencent.qcloud.tuikit.tuichat.classicui.interfaces.IChatLayout
import com.tencent.qcloud.tuikit.tuichat.util.ChatMessageBuilder
import com.tencent.qcloud.tuikit.tuicontact.TUIContactService
import com.tencent.qcloud.tuikit.tuicontact.bean.ContactItemBean


class ChatAppModuleUtil {
    companion object {
        fun init(appActivity: BaseActivity) {
            initChatModule(appActivity)

        }

        private fun initChatModule(appActivity: BaseActivity) {
            var chatAppModule = ChatAppModule.init()
            chatAppModule.setCallback(object : AppModuleCallback {
                override fun onSendRedPackageActionCompleted(
                    view: IChatLayout,
                    isGroup: Boolean,
                    ID: String
                ) {
                    SendRedPackageMoneyActivity.start(appActivity, isGroup, ID,
                        object : SendRedPackageMoneyActivity.OnSendListener {
                            override fun onSucceed(customRedPackageMessage: CustomRedPackageMessage?) {
                                val gson = Gson()
                                val data = gson.toJson(customRedPackageMessage)
                                val info = ChatMessageBuilder.buildCustomMessage(
                                    data,
                                    customRedPackageMessage?.text,
                                    customRedPackageMessage?.text?.toByteArray()
                                )
                                view.sendMessage(info, false)
                            }
                        })
                }

                override fun onGetRedPackageActionCompleted(
                    context: Context,
                    msg: CustomRedPackageMessageBean
                ) {
                    var dialog = RedPackageDialog.Builder(context)
                    dialog.clickResult = object : RedPackageDialog.Builder.ClickResult {
                        override fun onConfirmClick() {
                            RedPackageActivity.start(context)
                        }

                    }
                    dialog.show()
                }

                override fun onBusinessCardActionCompleted(ID: String) {
                    val userIdList: MutableList<String> = ArrayList()
                    userIdList.add(ID)
                    V2TIMManager.getInstance().getUsersInfo(
                        userIdList,
                        object : V2TIMValueCallback<List<V2TIMUserFullInfo>> {
                            override fun onError(code: Int, desc: String) {}
                            override fun onSuccess(v2TIMUserFullInfos: List<V2TIMUserFullInfo>) {
                                val contactItemBeanList: MutableList<ContactItemBean> =
                                    ArrayList<ContactItemBean>()
                                val contactItemBean = ContactItemBean()
                                for (userFullInfo in v2TIMUserFullInfos) {
                                    contactItemBean.setNickName(userFullInfo.nickName)
                                    contactItemBean.setId(userFullInfo.userID)
                                    contactItemBean.setAvatarUrl(userFullInfo.faceUrl)
                                    contactItemBean.setSignature(userFullInfo.selfSignature)
                                    contactItemBeanList.add(contactItemBean)
                                }
                                val intent = Intent(
                                    TUIContactService.getAppContext(),
                                    FriendHomeActivity::class.java
                                )
                                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                                intent.putExtra(
                                    FriendHomeActivity.INTENT_USER_ID,
                                    contactItemBean.id
                                )
                                TUIContactService.getAppContext().startActivity(intent)
                            }
                        })
                }
            })
        }
    }

}